unit fCompanyManage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, fBaseManage, dxBar, cxClasses, StdCtrls, VirtualTrees, ExtCtrls,
  uDAScriptingProvider, uDADataTable, uDAMemDataTable, uPublic, Menus;

type
  TfmCompanyManage = class(TfmBaseManage)
    procedure FormCreate(Sender: TObject);
    procedure vstItemsGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
    procedure vstItemsFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Initialize; override;
    procedure OnRefreshCompany(var AMessage: TMessage); message WM_REFRESH_TREE;     
  end;

var
  fmCompanyManage: TfmCompanyManage;

implementation

uses
  fCompanyInfo;

{$R *.dfm}

const Modal = 'Company';

type TCompanyNodeData = record
  Id: TGuid;
  No: string;
  Simpname: string;
end;

type PCompanyNodeData = ^TCompanyNodeData;

procedure TfmCompanyManage.OnRefreshCompany(var AMessage: TMessage);
begin
  Initialize;
  vstItems.SetFocus;
end;

procedure TfmCompanyManage.Initialize;
var
  node: PVirtualNode;
  nd: PCompanyNodeData;
begin
  vstItems.Clear;
  InitDataSets;

  if not tblBase.IsEmpty then
  begin
    tblBase.First;
    while not tblBase.EOF do
    begin
      node := vstItems.AddChild(nil);
      nd := vstItems.GetNodeData(node);
      nd^.Id := tblBase.FieldByName('bo_id').AsGuid;
      nd^.No := tblBase.FieldByName('bo_no').AsString;
      nd^.Simpname := tblBase.FieldByName('bo_simpname').AsString;
      tblBase.Next;
    end;
  end;

  CloseDataSets;  
end;

procedure TfmCompanyManage.FormCreate(Sender: TObject);
begin
  inherited;
  lblDetailTitle.Visible := False;
  vstItems.NodeDataSize := SizeOf(TCompanyNodeData);
  FSql := 'select bo_id, bo_no, bo_simpname from b_company';

  Initialize;

  if vstItems.GetFirst <> nil then
  begin
    vstItems.Selected[vstItems.GetFirst] := True;
    vstItemsFocusChanged(vstItems, vstItems.GetFirst,0);
  end
//  else
//  begin
//    ShowEmbedForm(TfmCompanyInfo, pnlRight, fatNew, StringToGuid('{00000000-0000-0000-0000-000000000000}'));
//  end;

end;

procedure TfmCompanyManage.vstItemsFocusChanged(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex);
var
  nd: PCompanyNodeData;
begin
  nd := Sender.GetNodeData(Node);
  ShowEmbedForm(TfmCompanyInfo,  pnlRight,fatView, nd^.Id);  
end;

procedure TfmCompanyManage.vstItemsGetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: WideString);
var
  nd: PCompanyNodeData;
begin
  inherited;
  nd := Sender.GetNodeData(Node);
  CellText := nd^.Simpname;
end;

end.
